package com.xhsj.user.pms.patent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.pms.patent.domain.Bill;
import com.xhsj.user.pms.patent.domain.BillFile;
import com.xhsj.user.pms.patent.domain.BillList;
import com.xhsj.user.pms.patent.domain.ExamineModel;
import com.xhsj.user.pms.patent.mapper.BillFileMapper;
import com.xhsj.user.pms.patent.mapper.BillMapper;
import com.xhsj.user.pms.patent.service.BillService;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 苏小林
 * @Description
 * @createTime 2020年02月20日 10:41:00
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;
    @Autowired
    private BillFileMapper billFileMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveBill(Bill bill) {
        // 缴费单主键id
        String billInformId = IdGen.generateUUID();
        bill.setBillInformId(billInformId);
        bill.setCreatedTime(new Date());
        bill.setUpdateTime(new Date());
        billMapper.insert(bill);
        BillFile billFile = bill.getBillFile();
        // 文件主键id
        billFile.setBillFileId(IdGen.generateUUID());
        billFile.setBillId(billInformId);
      return  billFileMapper.insert(billFile);

    }

    @Override
    public IPage<BillList> findBillList(Map<String, Object> data) {
        int pageNum = 1;
        int pageSize = 20;
        if (StringUtils.isNotNull(data.get("pageNum")))
            pageNum = Integer.parseInt(String.valueOf(data.get("pageNum")));
        if (StringUtils.isNotNull(data.get("pageSize")))
            pageSize = Integer.parseInt(String.valueOf(data.get("pageSize")));

        String type = String.valueOf(data.get("type"));
        // 专利名称
        String name = Objects.isNull(data.get("name"))?"":String.valueOf(data.get("name"));
        // 发明人
        String inventor = Objects.isNull(data.get("inventor"))?"":String.valueOf(data.get("inventor"));
        // 申请人
        String fullName = Objects.isNull(data.get("fullName"))?"":String.valueOf(data.get("fullName"));
        IPage<BillList> page = new Page<>(pageNum, pageSize);
        // 查询全部
        if ("1".equals(type)) {
            return billMapper.findBillList(page,name,inventor,fullName);
        }

        // 查询快到期
        if ("2".equals(type)) {
            return billMapper.findExpiringList(page,name,inventor,fullName);
        }

        // 查询已过期
        if ("3".equals(type)) {
            return billMapper.findexpiredList(page,name,inventor,fullName);
        }

        // 查询已续费
        if ("4".equals(type)) {
            return billMapper.findRenewalList(page,name,inventor,fullName);
        }
        return null;
    }

    @Override
    public List<BillList> billDetailByDisclosure(String disclosureId) {
        // 根据交底书id 查询缴费详情
        return billMapper.billDetailByDisclosure(disclosureId);
    }

    @Override
    public void delByIds(List<String> list) {
        for (String s : list) {
            billMapper.deleteById(s);
            billFileMapper.delByBillId(s);
        }
    }

    @Override
    public List<Map<String, Object>> getBillByDisclosureId(String billForDisclosure) {
        return billMapper.getBillByDisclosureId(billForDisclosure);
    }
}
